<?php
/**
 * @name 发货单控制器
 * @Author : yu
 * @Created : 2016-11-2
 * @Copyright : MPE.Fiidee.Monda.Cn
 */
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;


class InvoiceAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();	
	}
	protected $serviceBean="trading.tradingInvoice.service";

	public function index(HttpRequest $request)
	{
		$saleService = Beans::get($this->serviceBean);
		$contractService = Beans::get('trading.tradingSale.service');
		$orderService = Beans::get('trading.tradingOrder.service');

		$page = $request->getParameter('page');
		$pagesize = $request->getParameter('pagesize');

		if ($page <= 0) $page = 1;
		if ($pagesize <= 0) $pagesize = 15;
		$this->page=$page;
		$this->pagesize=$pagesize;


		$search_arr = array(
			"select_mix_option1" => $request->getParameter('select_mix_option1'),
			"select_mix_option_value1" => urldecode($request->getParameter('select_mix_option_value1')),
			"select_mix_option2" => $request->getParameter('select_mix_option2'),
			"start_time" => $request->getParameter('start_time'),
			"end_time" => $request->getParameter('end_time'),
		);

        //导出Excel add By Laoliu
        if( $request -> getParameter('export')==1 ){
            $page =1;
            $pagesize =2147483648;
            $ids = $request -> getParameter('ids');
            if( $ids ){
                $tmp = "('0'";
                foreach ($ids as $value){
                    $tmp .= ','."'$value'";
                }
                $search_arr['ids']=$tmp.')';
            }
        }

		$arr = $saleService->search($pagesize, $page, $search_arr);

		$items = $arr['items'];

		$itemsLen = count($items);

		for ($i = 0; $i < $itemsLen; $i++) {

			$items[$i]['shipping_way'] = $orderService->getShippingwayText($items[$i]['shipping_way']);
			$items[$i]['pay_way'] = $orderService->getPaywayText2($items[$i]['pay_way']);
		}

        //var_dump($items);
        //导出Excel add By Laoliu
        if( $request->getParameter('export') == 1){
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["invoice_no"];
                $tmp[] = $v["username"];
                $tmp[] = $v["order_no"];
                $tmp[] = $v["actual_amount"];
                $tmp[] = $v["delivery_time"];
//                $tmp[] = $v["consignee"];
//                $tmp[] = $v["receipt_address"];
//                $tmp[] = $v["contact_mobile"];
                $tmp[] = $v["pay_way"];
                $tmp[] = $v["shipping_way"];
                $excel[] = $tmp;
            }
            $header = array('发货单号', '买家会员号', '订单号', '销售金额(￥)', '发货时间', '支付方式','配送方式');
            phpexcel("发货单列表",$header,$excel);
            exit();
        }

		$this->generatePageMenu($arr['total']);

		$this->assign('items', $items);
		$this->assign('search_arr', $search_arr);

		//$this->assign('_table', '发货单');
		//$this->assign('_filter', array('contract_no', 'sellers', 'buyers', 'order_no', 'signed_time', 'type', 'status'));
		//$this->assign('_header', array('合同号', '卖家', '买家', '订单号', '签订时间', '类型', '状态'));


		$this->assign("title", "发货单列表");
		$this->assign('bread', array('报表管理',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/index');
	}
	

}
